/**
 * 
 */
package com.surthi.app.magazine.dao.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.surthi.app.magazine.dao.DocumentAccess;
import com.surthi.app.magazine.model.Document;

/**
 * @author Vdevi
 *
 */
public class DocumentAccessImpl implements DocumentAccess{
	private String basePath = System.getProperty("java.io.tmpdir")+"/magazine";
	
	public Boolean createDocument(final Document doc){
		if(doc==null)
			throw new NullPointerException();
		boolean dirCreated = dirMaintainance(basePath+"/"+doc.getYear()+"/"+doc.getMonth()+"/"+doc.getFileName());
		if(dirCreated){
			return writeIntoFile(doc);
		}
		return false;
	}

	public Boolean updateDocument(final Document doc) {
		if(doc==null){
			throw new NullPointerException();
		}
		boolean fileExists = dirUpdateMaintainance(basePath+"/"+doc.getYear()+"/"+doc.getMonth()+"/"+doc.getFileName());
		if(fileExists){
			return writeIntoFile(doc);
		}
		return false;
	}

	public Boolean deleteDocument(final String year,final String month,final String fileName) {
		if(year==null || year.equals("") || month==null || month.equals("") || fileName==null|| fileName.equals("")){
			throw new NullPointerException();
		}
		File file = new File(basePath+"/"+year+"/"+month+"/"+fileName);
		if(file.exists()){
			return file.delete();
		}
		return false;
	}

	public Boolean writeIntoFile(final Document doc){
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = doc.getInStream();
			out = new FileOutputStream(basePath+"/"+doc.getYear()+"/"+doc.getMonth()+"/"+doc.getFileName());
			int c;
			while ((c = in.read()) != -1) {
				out.write(c);
			}
		}catch(Exception e){
			return false;
		}finally {
			try{
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
				if(doc.getInStream() !=null){
					doc.getInStream().close();
				}
			}catch(Exception e){}
		}
		return true;
	}
	
	public Boolean dirMaintainance(final String path){
		if(path==null || path.equals("")){
			throw new NullPointerException();
		}
		File targetFile = new File(path);
		File parent = targetFile.getParentFile();
		if(parent==null){
			throw new NullPointerException();
		}
		if(!parent.exists() && !parent.mkdirs()){
			throw new IllegalStateException("Couldn't create dir: " + parent);
		}else{
			try {
				if(!targetFile.exists()){
					targetFile.createNewFile();
				}else{
					targetFile.delete();
					targetFile.createNewFile();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return targetFile.exists();
	}
	
	public Boolean dirUpdateMaintainance(final String path){
		if(path==null || path.equals("")){
			throw new NullPointerException();
		}
		File targetFile = new File(path);
		File parent = targetFile.getParentFile();
		if(parent==null){
			throw new NullPointerException();
		}
		if(targetFile.exists()){
			try {
				targetFile.delete();
				targetFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return targetFile.exists();
	}
}
